package edu.gatech.fiveminutesleft;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import edu.gatech.fiveminutesleft.model.LocationName;
import edu.gatech.fiveminutesleft.model.Task;

/**
 * This is the Activity which allows users to view the details of a Task (and provides a button for
 * editing).
 * 
 * @author Garret Malmquist
 */
public class TaskViewActivity extends Activity {

	/**
	 * Launches this activity
	 * 
	 * @param source
	 *            the activity that called launch
	 * @param task
	 *            - the Task containing the data to populate the gui fields with
	 */

	public static void launch(Activity source, Task task, String[] categories) {
		Intent n = new Intent(source, TaskViewActivity.class);
		GUISpace.put(TaskViewActivity.class, "task", task);
		GUISpace.put(TaskViewActivity.class, "categories", categories);

		source.startActivity(n);
	}

	private TextView	nameView, dueView, timeView, locationView, repeatView, notesView, tagsView;
	private ImageButton	mapButton;
	private Task		task;

	@Override
	/**
	 * Sets the layout of this activity and initializes GUI instance variables
	 */
	public void onCreate(Bundle b) {
		super.onCreate(b);

		this.task = (Task) GUISpace.get(getClass(), "task");

		setContentView(R.layout.task_view_layout);

		nameView = (TextView) findViewById(R.id.TaskName);
		dueView = (TextView) findViewById(R.id.TaskDueDate);
		timeView = (TextView) findViewById(R.id.TaskDueTime);
		locationView = (TextView) findViewById(R.id.TaskLocationText);
		repeatView = (TextView) findViewById(R.id.TaskRepeat);
		notesView = (TextView) findViewById(R.id.TaskNotes);
		tagsView = (TextView) findViewById(R.id.TaskTags);
		mapButton = (ImageButton) findViewById(R.id.ViewMapButton);

	}

	@Override
	public void onResume() {
		super.onResume();
		this.task = (Task) GUISpace.get(getClass(), "task");
		updateValues();
	}

	/**
	 * Clear out any remaining persistent data when this task is done.
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		if (isFinishing()) {
			GUISpace.clearPrefix(getClass());
		}
	}

	/**
	 * Standard Android method. Called when the physical menu button pressed.
	 * 
	 * @return returns true to make the menu display (if false returned menu wont display)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.task_menu, menu);
		return true;
	}

	/**
	 * Standard Android method. Called when one of the options on the android menu (collection of
	 * options displayed after the physical menu button is pressed) is pressed. Currently logs out
	 * if that option selected, and does nothing for other options.
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
			case R.id.editTask:
				editTask();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * Updates the GUI fields of this Activity
	 */
	private void updateValues() {
		if (task == null) {
			Log.e("TaskViewActivity", "Task is null");
			return;
		}

		nameView.setText(task.getName());

		if (task.getDate() != null && task.getDate().trim().length() != 0)
			dueView.setText(task.getDate());
		else
			dueView.setText("No date set");

		if (task.getTime() != null && task.getTime().trim().length() != 0)
			timeView.setText(task.getTime());
		else
			timeView.setText("No time set");

		if (task.getLocation() != null && task.getLocation().trim().length() != 0) {
			locationView.setText(task.getLocation());
			mapButton.setVisibility(View.VISIBLE);
			mapButton.setEnabled(true);
		} else {
			locationView.setText("No location set");
			mapButton.setVisibility(View.INVISIBLE);
			mapButton.setEnabled(false);
		}

		repeatView.setText(task.getRepeats());
		notesView.setText(task.getNotes());
		tagsView.setText(task.getTags());

	}

	public void edit(View view) {
		editTask();
	}

	/**
	 * Calls up the Activity to edit the details of this task when the "Edit" button is pressed
	 * 
	 * @param view
	 */
	public void editTask() {
		TaskEditActivity.launch(this, this.task, (String[]) GUISpace.get(getClass(), "categories"));
	}

	/**
	 * Calls up Google Maps to display the geographic location of this task when "View Location" is
	 * pressed
	 * 
	 * @param view
	 */
	public void viewMap(View view) {
		LocationViewActivity.launch(this, LocationName.getFromTask(task), false);
	}
}
